package com.njworkorder.Controller;

import cn.microvideo.qsc.client.api.QsApi;
import cn.microvideo.qsc.client.entity.OrganizationInfo;
import cn.microvideo.qsc.client.entity.Role;
import com.njworkorder.Entity.AdministrativeOrganization;
import com.njworkorder.HTSingleLogin.QsUser;
import com.njworkorder.HTSingleLogin.SecurityUtils;
import com.njworkorder.Service.AdministrativeOrganizationService;
import com.njworkorder.Utils.Result;
import com.njworkorder.Utils.ResultUtil;
import com.njworkorder.adapter.QsApiAdapter;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.core.Authentication;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/cas-api")
@Slf4j
public class CasController {

    @Value("${cas.client-redirect-url}")
    private String redirectUrl;

    @Resource
    QsApiAdapter qsApiAdapter;

    private final AdministrativeOrganizationService administrativeOrganizationService;
    public CasController(AdministrativeOrganizationService administrativeOrganizationService) {
        this.administrativeOrganizationService = administrativeOrganizationService;
    }


    @GetMapping()
    @ResponseBody
    @Operation(summary = "cas-api",description = "cas-api")
    public void casApi(HttpServletResponse response) throws Exception {
        Authentication authentication = SecurityUtils.getAuthentication();
        if (authentication != null&&authentication.isAuthenticated()) {
            log.info("认证==={}",authentication.getName());
        }else {
            log.info("未认证");
        }
        log.info("cas-api redirect url: {}", redirectUrl);
        response.sendRedirect(redirectUrl);
    }


    @Operation(summary = "获取用户信息",description = "获取用户信息")
    @GetMapping("/getInfo")
    public Result<QsUser> getInfo() {
        QsUser qsUser = SecurityUtils.getLoginUser().getQsUser();
        return ResultUtil.success(qsUser);
    }

    /**
     * 获取用户角色
     */

    @Operation(summary = "当前用户获取用户角色",description = "当前用户获取用户角色")
    @GetMapping("/getRoles")
    public Result<List<String>> getRoles() {
//        String userId = SecurityUtils.getUserId();
        List<Role> personRole = qsApiAdapter.findPersonRole("13851918117");
        log.info("personRole==={}",personRole);
        return ResultUtil.success("");
    }

    /**
     * 获取组织架构
     */
    @Operation(summary = "获取组织架构",description = "获取组织架构")
    @GetMapping("/getOrgs")
    @Transactional
    public Result<List<OrganizationInfo>> getOrgs() {
        //获取组织架构
        List<OrganizationInfo> organizationAll = QsApi.getApi().getOrganizationAll();
       // organizationAll.forEach(System.out::println);

//        List<AdministrativeOrganization> list = new ArrayList<>();
//        AdministrativeOrganization organization;
//        for (OrganizationInfo organizationInfo : organizationAll) {
//            organization = new AdministrativeOrganization();
//            organization.setId(organizationInfo.getId());
//            organization.setXzzzid(organizationInfo.getId());
//            organization.setName(organizationInfo.getName());
//            organization.setOCode(organizationInfo.getoCode());
//            organization.setType(organizationInfo.getType());
//            organization.setShortNameExt(organizationInfo.getShortNameExt1());
//            organization.setParentId(organizationInfo.getParent());
//
//            list.add(organization);
//        }

        //administrativeOrganizationService.saveBatch(list);

        return ResultUtil.success(organizationAll);
    }




}
